#include "featureextractor.h"
#include <boost/foreach.hpp>
#include "stitcher.h"
#include "inputimage.h"
#include "sift/sift.h"
#include "sift/imgfeatures.h"
#include "imagefeature.h"
#include <opencv/highgui.h>
#include "imageset.h"

using namespace cshotel;
using namespace boost;
using namespace std;
using namespace sift;
using namespace cv;

FeatureExtractor::FeatureExtractor(shared_ptr<ImageSet> imgs) : imageSet(imgs) {}

void FeatureExtractor::extract() {
    dout("Extracting features ..." << endl);
    
    // iterate over all images
    foreach(shared_ptr<InputImage> image, imageSet->getImages()) {
        dout("    image " << image->getId() << " ... ");
        feature* feat = 0;
        shared_ptr<Mat> matchingVersionImageFile = image->getMatchingVersionImage();
        
        // extract the features for each image
        // we use the scaled down version for this
        IplImage iplImage = static_cast<IplImage>(*matchingVersionImageFile);
        int numfeat = sift_features(&iplImage, &feat);
        for(int ii = 0; ii < numfeat; ++ii) {
            shared_ptr<ImageFeature> p(new ImageFeature(feat[ii], image));
            image->addImageFeature(p);
        }
        
        dout("found " << numfeat << " features" << endl);
        free(feat);
    }
}
